﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.WinForms;
using RM2012Server.Controller;
using RM2012Server.Model;
using RM2012Server.Business;

namespace RM2012Server.View
{
    public partial class frmTables : Office2007Form
    {
        #region Fields
        TablesCtrl m_TablesCtrl = new TablesCtrl();
        AreaCtrl m_AreaCtrl = new AreaCtrl();
        StateCtrl m_StateCtrl = new StateCtrl();
        public static bool isNew = false;
        #endregion

        #region Constructor
        public frmTables()
        {
            InitializeComponent();
        }
        #endregion

        #region Load
        private void frmTables_Load(object sender, EventArgs e)
        {
            m_AreaCtrl.LoadToComboBox(cmbTKArea);
            m_StateCtrl.LoadToComboBox(cmbTKState);
            m_AreaCtrl.LoadToComboBox(cmbTKAAreaId);
            m_StateCtrl.LoadToComboBox(cmbTKAState);
            m_StateCtrl.LoadToComboBox(cmbState);
            m_StateCtrl.LoadToDataGridViewComboBoxColumn(colStateId);
            m_AreaCtrl.LoadToComboBox(cmbArea);
            m_AreaCtrl.LoadToDataGridViewComboBoxColumn(colAreaId);
            m_TablesCtrl.Show(dgvTables, bindingNavigatorTables, cmbArea, txtTablesId, txtTablesName, cmbState, txtRemark);
            SaveItem.Enabled = false;
            isNew = false;
        }
        #endregion

        #region Button Event Click
        private void btnAddArea_Click(object sender, EventArgs e)
        {
            ShowForm.ShowFormArea();
        }

        private void btnAddState_Click(object sender, EventArgs e)
        {
            ShowForm.ShowFormState();
        }

        private void btnDongY_Click(object sender, EventArgs e)
        {
            TablesInfo info = new TablesInfo();
            info.TablesId = txtTablesId.Text;
            info.TablesName = txtTablesName.Text;
            info.StateId = Convert.ToInt32(cmbState.SelectedValue);
            info.AreaId = cmbArea.SelectedValue.ToString();
            info.Remark = txtRemark.Text;
            if (isNew == true)
            {
                m_TablesCtrl.Add(info);
            } 
            else
            {
                m_TablesCtrl.Update(info);
            }
            m_TablesCtrl.Show(dgvTables, bindingNavigatorTables, cmbArea, txtTablesId, txtTablesName, cmbState, txtRemark);
            SaveItem.Enabled = false;
            isNew = false;
        }

        private void btnLamLai_Click(object sender, EventArgs e)
        {
            isNew = false;
            m_TablesCtrl.Show(dgvTables, bindingNavigatorTables, cmbArea, txtTablesId, txtTablesName, cmbState, txtRemark);
            SaveItem.Enabled = false;
        }
        #endregion

        #region BindingNavigator Item Click
        private void AddNewItem_Click(object sender, EventArgs e)
        {
            isNew = true;
            if (dgvTables.Rows.Count == 0)
            {
                DeleteItem.Enabled = false;
            }
            DataRow m_Row = m_TablesCtrl.AddNewRow();
            m_Row["TablesId"] = GetID.Tables(dgvTables);
            m_Row["TablesName"] = "";
            m_Row["StateId"] = 1;
            m_Row["AreaId"] = "";
            m_Row["Remark"] = "";
            m_TablesCtrl.AddNewTables(m_Row);
            bindingNavigatorTables.BindingSource.MoveLast();
            SaveItem.Enabled = true;
        }

        private void RefreshItem_Click(object sender, EventArgs e)
        {
            isNew = false;
            m_TablesCtrl.Show(dgvTables, bindingNavigatorTables, cmbArea, txtTablesId, txtTablesName, cmbState, txtRemark);
            SaveItem.Enabled = false;
        }

        private void DeleteItem_Click(object sender, EventArgs e)
        {
            isNew = false;
            if (dgvTables.Rows.Count == 0)
            {
                DeleteItem.Enabled = false;
            }
            else if (dgvTables.SelectedRows.Count > 0)
            {
                if (MessageBoxEx.Show("Bạn có chắc chắn xóa?", "XÓA", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    foreach (DataGridViewRow row in dgvTables.SelectedRows)
                    {
                        bindingNavigatorTables.BindingSource.RemoveAt(row.Index);
                    }
                    SaveItem.Enabled = true;
                }
            }
        }

        private void SaveItem_Click(object sender, EventArgs e)
        {
            isNew = false;
            bindingNavigatorPositionItem.Focus();
            bool check = true;
            foreach (DataGridViewRow row in dgvTables.Rows)
            {
                if (row.Cells["colTablesId"].Value.ToString() == "" || row.Cells["colTablesName"].Value.ToString() == "" || row.Cells["colStateId"].Value.ToString() == "" || row.Cells["colAreaId"].Value.ToString() == "")
                {
                    check = false;
                }
            }
            if (check)
            {
                if (!m_TablesCtrl.SaveData())
                {
                    MessageBoxEx.Show("Không thể thực hiện !", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    SaveItem.Enabled = false;
                }
            }
            else
            {
                MessageBoxEx.Show("Dữ liệu không hợp lệ !", "HÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void ExitItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region DataGridViewX Event
        private void dgvTables_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            MessageBox.Show("Dữ liệu không hợp lệ !", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void dgvTables_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            SaveItem.Enabled = true;
        }
        #endregion

        #region Searching
        private void rbAreaId_CheckedChanged(object sender, EventArgs e)
        {
            if (rbAreaId.Checked == true)
            {
                lblKeyWord.Text = "Khu vực :";
                cmbTKArea.Visible = true;
                cmbTKState.Visible = false;
                txtKeyWord.Visible = false;
            }
        }

        private void rbState_CheckedChanged(object sender, EventArgs e)
        {
            if (rbState.Checked == true)
            {
                lblKeyWord.Text = "Trạng thái :";
                cmbTKArea.Visible = false;
                cmbTKState.Visible = true;
                txtKeyWord.Visible = false;
            }
        }

        private void rbTableId_CheckedChanged(object sender, EventArgs e)
        {
            if (rbTableId.Checked == true)
            {
                lblKeyWord.Text = "Mã bàn :";
                cmbTKArea.Visible = false;
                cmbTKState.Visible = false;
                txtKeyWord.Visible = true;
            }
        }

        private void rbTablesName_CheckedChanged(object sender, EventArgs e)
        {
            if (rbTablesName.Checked == true)
            {
                lblKeyWord.Text = "Tên bàn :";
                cmbTKArea.Visible = false;
                cmbTKState.Visible = false;
                txtKeyWord.Visible = true;
            }
        }

        private void btnTimKiemTables_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dtSource = new DataTable();
                if (rbTableId.Checked == true)
                {
                    dtSource = m_TablesCtrl.GetByLikeId(txtKeyWord.Text);
                }
                else if (rbTablesName.Checked == true)
                {
                    dtSource = m_TablesCtrl.GetByName(txtKeyWord.Text);
                }
                else if (rbAreaId.Checked == true)
                {
                    dtSource = m_TablesCtrl.GetByArea(cmbTKArea.SelectedValue.ToString());
                }
                else if (rbState.Checked == true)
                {
                    dtSource = m_TablesCtrl.GetByState(Convert.ToInt32(cmbTKState.SelectedValue.ToString()));
                }
                else
                {
                    dtSource = m_TablesCtrl.GetByName(txtKeyWord.Text);
                }

                m_TablesCtrl.ShowWithSources(dgvTables, bindingNavigatorTables, cmbArea, txtTablesId, txtTablesName, cmbState, txtRemark, dtSource);
                SaveItem.Enabled = false;
            }
            catch
            {
                MessageBoxEx.Show("Không tìm thấy kết quả !", "HÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnTKA_Click(object sender, EventArgs e)
        {
            try
            {
                String AtableId = "";
                String AtableName = "";
                String AareaId = "-1";
                int AstateId = -1;

                if (chkTablesId.Checked == true)
                {
                    AtableId = txtTKTablesID.Text;
                }
                if (chkTablesName.Checked == true)
                {
                    AtableName = txtTKTablesName.Text;
                }
                if (chkAreaId.Checked == true)
                {
                    AareaId = cmbTKAAreaId.SelectedValue.ToString();
                }
                if (chkState.Checked == true)
                {
                    AstateId = Convert.ToInt32(cmbTKAState.SelectedValue.ToString());
                }

                DataTable dtSource = m_TablesCtrl.GetByOption(AtableId, AtableName, AareaId, AstateId);
                m_TablesCtrl.ShowWithSources(dgvTables, bindingNavigatorTables, cmbArea, txtTablesId, txtTablesName, cmbState, txtRemark, dtSource);
                SaveItem.Enabled = false;
            }
            catch
            {
                MessageBoxEx.Show("Không tìm thấy kết quả !", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        #endregion

        #region Context Menu Event Click
        private void AddNewItemContext_Click(object sender, EventArgs e)
        {
            AddNewItem_Click(sender, e);
        }

        private void RefreshItemContext_Click(object sender, EventArgs e)
        {
            RefreshItem_Click(sender, e);
        }

        private void DeleteItemContext_Click(object sender, EventArgs e)
        {
            DeleteItem_Click(sender, e);
        }

        private void SaveItemContext_Click(object sender, EventArgs e)
        {
            SaveItem_Click(sender, e);
        }

        private void ExitItemContext_Click(object sender, EventArgs e)
        {
            ExitItem_Click(sender, e);
        }
        #endregion
    }
}
